import { ref } from 'vue'
import type pageModal from '@/components/page-modal/page-modal.vue'

type CallbackFnType = (data?: any) => void

function usePageModal(newCallback?: CallbackFnType, editCallback?: CallbackFnType) {
  // 点击content，modal的操作
  const modalRef = ref<InstanceType<typeof pageModal>>()
  function handleNewClick() {
    // 1.让modal显示出来
    modalRef.value?.setModaVisible()

    // 2.新建的回调
    if (newCallback) newCallback()
  }
  function handleEditClick(itemData: any) {
    // 1.让modal显示出来
    modalRef.value?.setModaVisible(false, itemData)
    // 2.编辑的回调
    if (editCallback) editCallback(itemData)
  }

  return {
    modalRef,
    handleNewClick,
    handleEditClick
  }
}

export default usePageModal
